import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * This class is the mainplayer controller
 * @author Sravya
 *
 */
public class MainPlayerController 
{
	private MainPlayerView mv;
	private MainPlayerModel mm;
	private boolean hasUserPressedNext;
	
	public MainPlayerController(MainPlayerView mv, MainPlayerModel mm)
	{
		this.mv = mv;
		this.mm = mm;
		hasUserPressedNext = false;
		mv.addNextButtonListener(new nextButtonListener());
	}
	/**
	 * This methods adds cash
	 * @param cash
	 */
	public void addCash(double cash)
	{
		mm.addToCash(cash);
	}
	/**
	 * This method is called when the user pressed next
	 * @return
	 */
	public boolean hasUserPressedNext()
	{
		return hasUserPressedNext;
	}
	/**
	 * this is the buttonListener for the next button
	 * @author paulsmith
	 *
	 */
	private class nextButtonListener implements ActionListener
	{	
		public void actionPerformed(ActionEvent e) 
		{
			mm.setPartyMembers(mv.getPartyMembersNames());
			String playerName = mv.getMainPlayerName();
			
			if(playerName.equals(""))
			{
				JOptionPane.showMessageDialog(new JFrame("Important!"),
						"You must choose a name for the leader!");
				return;
			}
			
			mm.setName(playerName);
			
			int index = mv.getProfessionIndex();
			System.out.println("index: " + index);
			
			if(index == -1)
			{
				JOptionPane.showMessageDialog(new JFrame("Important!"),
						"You must select a profession!");
				return;
			}
			
			mm.setProfession(index);
			mm.calcInitialCash();
			System.out.println("cash: " + mm.getTotalCash());
			hasUserPressedNext = true;
		}

	}
	public double getCash() {
		return mm.getTotalCash();
	}
}
